package edu.nf.relaxenjoy.service.impl;

import com.github.pagehelper.PageInfo;
import edu.nf.relaxenjoy.common.util.ConvertUtils;
import edu.nf.relaxenjoy.domain.dto.TagDTO;
import edu.nf.relaxenjoy.domain.entity.Post;
import edu.nf.relaxenjoy.domain.entity.Tag;
import edu.nf.relaxenjoy.domain.entity.TagRelation;
import edu.nf.relaxenjoy.domain.entity.User;
import edu.nf.relaxenjoy.domain.vo.PostVo;
import edu.nf.relaxenjoy.domain.vo.TagVo;
import edu.nf.relaxenjoy.mapper.PostMapper;
import edu.nf.relaxenjoy.mapper.TagMapper;
import edu.nf.relaxenjoy.mapper.TagRelationMapper;
import edu.nf.relaxenjoy.mapper.UserMapper;
import edu.nf.relaxenjoy.service.TagService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhu
 * @date 2024-12-19
 */
@Service
@RequiredArgsConstructor
public class TagServiceImpl implements TagService {

    private final TagMapper tagMapper;

    private final TagRelationMapper tagRelationMapper;

    private final PostMapper postMapper;

    private final UserMapper userMapper;

    @Override
    public List<TagVo> getAllTag() {
        List<Tag> tagList = tagMapper.getList();
        return ConvertUtils.sourceToTarget(tagList, TagVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Tag saveTag(TagDTO tagDTO) {
        Tag tag = tagMapper.getByName(tagDTO.getName());
        if (tag != null) {
            tag.setCount(tag.getCount() + 1);
            tagMapper.update(tag);
            return tag;
        } else {
            Tag tagEntity = ConvertUtils.sourceToTarget(tagDTO, Tag.class);
            tagEntity.setCount(1);
            tagEntity.setCreateDate(new Date());
            tagMapper.insert(tagEntity);
            return tagMapper.getByName(tagEntity.getName());
        }
    }

    @Override
    public long saveTagByName(String name) {
        Tag tag = tagMapper.getByName(name);
        if (tag != null) {
            tag.setCount(tag.getCount() + 1);
            tagMapper.update(tag);
            return tag.getId();
        } else {
            Tag tagEntity = new Tag();
            tagEntity.setName(name);
            tagEntity.setCount(1);
            tagMapper.insert(tagEntity);
            return tagEntity.getId();
        }
    }

    @Override
    public PageInfo<PostVo> getImgListByTag(long page, long limit, String id, Integer type) {
        PageInfo<PostVo> pages = new PageInfo<>();
        PageInfo<Post> postPageInfo;
        List<TagRelation> tagRelationList = tagRelationMapper.getListByTid(Long.valueOf(id));
        List<Long> mids = tagRelationList.stream().map(TagRelation::getMid).collect(Collectors.toList());

        if (type == 0) {
            List<Post> postList = postMapper.listByIds(mids, null);
            postList.sort((a,b) -> b.getCreateDate().compareTo(a.getCreateDate()));
            //获取最新
            postPageInfo = new PageInfo<>(postList);
        } else {
            List<Post> postList = postMapper.listByIds(mids, null);
            postList.sort((a,b) -> b.getAgreeCount().compareTo(a.getAgreeCount()));
            postPageInfo = new PageInfo<>(postList);
        }

        List<Post> postList = postPageInfo.getList();
        List<Long> uids = new ArrayList<>();
        postList.forEach(item -> {
            uids.add(item.getUserId());
        });

        List<User> userList = userMapper.listByIds(uids);
        HashMap<Long, User> userMap = new HashMap<>();
        userList.forEach(item -> {
            userMap.put(item.getUserId(), item);
        });

        List<PostVo> res = new ArrayList<>();
        PostVo postVo;
        User user;
        for (Post model : postPageInfo.getList()) {
            postVo = ConvertUtils.sourceToTarget(model, PostVo.class);
            user = userMap.get(model.getUserId());
            postVo.setUserId(user.getUserId());
            postVo.setUsername(user.getUsername());
            postVo.setAvatar(user.getAvatar());
            res.add(postVo);
        }
        pages.setPageNum(postPageInfo.getPageNum());
        pages.setPageSize(postPageInfo.getPageSize());
        pages.setIsFirstPage(postPageInfo.isIsFirstPage());
        pages.setIsLastPage(postPageInfo.isIsLastPage());
        pages.setList(res);
        pages.setTotal(postPageInfo.getTotal());

        return pages;
    }

    @Override
    public Tag getById(long id) {
        return tagMapper.getById(id);
    }
}
